/*
 * Atomic Predicates for Transformers
 * 
 * Copyright (c) 2015 UNIVERSITY OF TEXAS AUSTIN. All rights reserved. Developed
 * by: HONGKUN YANG and SIMON S. LAM http://www.cs.utexas.edu/users/lam/NRL/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * with the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimers.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimers in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the UNIVERSITY OF TEXAS AUSTIN nor the names of the
 * developers may be used to endorse or promote products derived from this
 * Software without specific prior written permission.
 * 
 * 4. Any report or paper describing results derived from using any part of this
 * Software must cite the following publication of the developers: Hongkun Yang
 * and Simon S. Lam, Scalable Verification of Networks With Packet Transformers
 * Using Atomic Predicates, IEEE/ACM Transactions on Networking, October 2017,
 * Volume 25, No. 5, pages 2900-2915 (first published as IEEE Early Access
 * Article, July 2017, Digital Object Identifier: 10.1109/TNET.2017.2720172).
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
 * THE SOFTWARE.
 */
package transformer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;

import common.PositionTuple;

public class TestIPinIPCOVE {
      
      public static void test() throws IOException, ClassNotFoundException
      {
            NetworkCOVE cove = null;
            /*
            if(new File(serialized).isFile()) 
            {
                  cove = NetworkCOVE.read_object(serialized);
            }else
            {
                  long t1 = System.nanoTime();
                  cove = new NetworkCOVE("cluster3");
                  long t2 = System.nanoTime();
                  System.out.println("in total: "+(t2-t1)+" ns");
                  cove.save_object();
            }*/
            
            cove = new NetworkCOVE("cluster3");
            
            for(PositionTuple pt : cove.topology.keySet())
            {
                  System.out.println(pt);
            }
            
            for(Box box : cove.boxes.values())
            {
                  System.out.println(box.name);
                  System.out.println(((Device) box).nat_addr);
            }

           //String[] path_info = {"29432", "1000", "719", "3", "29243", "29432"};
           
           cove.ini_IPinIP();
           cove.remove_null_routes();
           cove.add_ipinip_tunnel("719","29243", "29432");
           cove.add_ipinip_tunnel("719","29432", "29432");
           
           
           StateTransfer stfer = new StateTransfer(cove);
           StateBDD s = stfer.TraverseAnyNonTunnelPkt(new PositionTuple("29432", "719"));
           s.printState();
      }
      
      public static void test_ipinip(String name, int num) throws IOException, ClassNotFoundException
      {
            String net_name = name;
            String tunnel_file_name = net_name + "_tunnels";
            int tunnel_num = num;
            int tunnel_cnt = 0;
            
            NetworkCOVE cove = null;
            
             cove = new NetworkCOVE(net_name);
            
                        
            cove.ini_IPinIP();
            cove.remove_null_routes();
            
            FileInputStream fis = new FileInputStream(new File(tunnel_file_name));
            
            //Construct BufferedReader from InputStreamReader
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
       
            String line = null;
            while ((line = br.readLine()) != null) {
                  if(tunnel_cnt < tunnel_num)
                  {
                        tunnel_cnt ++;

                  }else
                  {
                        break;
                  }
                  String[] path_info = line.split(" ");
                  int path_len = path_info.length;
                  String entry = path_info[0];
                  String exit = path_info[path_len-2];
                  String destination = path_info[path_len-1];
                  cove.add_ipinip_tunnel(entry, exit, destination);
                  System.out.println(entry+","+exit+","+destination);

            }
       
            br.close();
                        
            String outputfolder = "ipinip_" + net_name + "/";
            File thedir = new File(outputfolder);
            if(!thedir.exists())
            {
                  thedir.mkdir();
            }
            
            HashSet<PositionTuple> allpts = cove.getallactiveports();
            HashSet<String> visited_boxes = new HashSet<String> ();
            HashSet<PositionTuple> usedpts = new HashSet<PositionTuple> ();
            
            for (PositionTuple pt : allpts)
            {
                  String box_name = pt.getPortName();
                  if(!visited_boxes.contains(box_name))
                  {
                        usedpts.add(pt);
                        visited_boxes.add(box_name);
                  }
            }
            System.out.println(usedpts.size()+" ports selected");
            
            //usedpts = new HashSet<PositionTuple> ();
            //usedpts.add(new PositionTuple("29049","8897"));
            //usedpts.add(new PositionTuple("15412","9116"));
            
            PrintWriter pw = new PrintWriter(new File(outputfolder + tunnel_num +"tunnels"));
            StateTransferI2Run.compute_trees_anynontunnelpkt(cove, usedpts, pw);
            pw.close();
      
      }
      
      public static void main(String[] args) throws IOException, ClassNotFoundException
      {
            test_ipinip(args[0], Integer.parseInt(args[1]));
            
            //test_ipinip("cluster304", 90);
            
            //test();
      }

}
